<?php


namespace App\Http\Controllers\Business;


use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\CommodityOrder;
use App\Models\CommodityOrderComment;
use App\Models\CommodityOrderDetail;
use App\Models\CustomerSessionInfo;
use App\Models\GoodsClass;
use App\Models\GoodsSku;
use App\Models\GoodsSpu;
use App\Models\GroupBuy;
use App\Models\Shop;
use App\Models\ShopCart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class CommodityController extends Controller
{
    public $internalPaymentSn;

    public function goodsClass(Request $request)
    {
        $classId = $request->input('class_id');
        $businessId = $request->input('business_id');
        $shop = Shop::where('business_id', $businessId)->first();
        if (!$shop) {
            return $this->makeResponse([], '店铺不存在', 404);
        }
        $shopId = $shop->id;


        $goodsClass = GoodsClass::with(['spu' => function ($qurey) use ($shopId) {
            $qurey->where('shop_id', $shopId);
        }]);
        if ($classId) {
            $goodsClass->where('id', $classId);
        }
        $data = $goodsClass->get();
        return $this->makeResponse($data, '查询成功', 200);
    }

    public function goodsDetail(Request $request)
    {
        $spuId = $request->input('spu_id', 0);
        $data = GoodsSpu::with('sku')->find($spuId);

        $data->comment = CommodityOrderComment::with(['customer' => function ($query) {
            $query->select('id', 'nick_name', 'avatar_url');
        }])->where('spu_id', $spuId)->limit(2)->get();
        $data->comment_count = CommodityOrderComment::where('spu_id', $spuId)->count();
        if (!$data) {
            return $this->makeResponse([], '查询失败', 404);
        }
        return $this->makeResponse($data, '查询成功', 200);

    }
}